package com.dferreira.languagesteach.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dferreira.languagesteach.R;
import com.dferreira.languagesteach.activities.generic.IFragmentStack;
import com.dferreira.languagesteach.constants.Constants;
import com.dferreira.languagesteach.data_adapters.LanguagesAdapter;
import com.dferreira.languagesteach.fragments.generic.NavigationListFragment;
import com.dferreira.languagesteach.helpers.GenericUI;
import com.dferreira.languagesteach.utils.DataPaths;
import com.dferreira.repository.Repository;
import com.dferreira.repository.data_models.PackageMetadata;

/**
 * Created by dferreira on 5/24/14.
 * for LanguagesTeach
 */
public class LanguagesFragment extends NavigationListFragment {
    private LanguagesAdapter languagesAdapter;

    private Boolean ATTACH_TO_ROOT = false;

    /**
     * On create install the required the languages package Sets the language
     * tableView dataSource asks the user if he want restore the last workout
     *
     * @param savedInstanceState If the activity is being re-initialized after
     *                           previously being shut down then this Bundle contains the data it most
     *                           recently supplied in {@link #onSaveInstanceState}.  <b><i>Note: Otherwise it is null.</i></b>
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        PackageMetadata packageMetadata;
        DataPaths dataPaths;
        Repository packagesRepository;
        String defaultPackage;
        Boolean buildResult;

        super.onCreate(savedInstanceState);

        packagesRepository = new Repository(getActivity());

        packagesRepository.prepare();

        dataPaths = new DataPaths(getActivity());
        if (dataPaths.searchUpdates()) {
            packagesRepository.updateRepositoryData();
        }

        defaultPackage = Constants.DEFAULT_PACKAGE;
        packageMetadata = packagesRepository.getInstalledPackageInfo(defaultPackage);


        languagesAdapter = new LanguagesAdapter(getActivity(), getFragmentStack(), packageMetadata.getInstalledPath());

        buildResult = languagesAdapter.buildDataSet();
        if (!buildResult) {
            packagesRepository.forceDefaultPackageInstallation();
            //Will try again after try install the default package
            buildResult = languagesAdapter.buildDataSet();
            if (!buildResult) {
                GenericUI.showToastMsg(getActivity(), getString(R.string.impossible_start));
            }
        }
        if (languagesAdapter.isDataSetBuilt()) {
            setListAdapter(languagesAdapter);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.navigation_list_fragment, container, ATTACH_TO_ROOT);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //Associate a list adapter
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    /**
     * Calls the languages resume from languages delegator that will treat
     * from the actions from the user
     */
    @Override
    public void onResume() {
        super.onResume();
        languagesAdapter.onResume();
    }

    /**
     * Called as part of the activity lifecycle when an activity is going into
     * the background, but has not (yet) been killed.
     * <p/>
     * call the on pause from the languages delegator and restore session
     */
    @Override
    public void onPause() {
        //setGlobalState(null);
        super.onPause();
        languagesAdapter.onPause();
    }
}
